package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.bean.SelectResourseBean;
import com.wwwarehouse.contract.common.ContractCommon;

import java.util.Iterator;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/22 20:23
 * @描述 ${TODO}
 */

public class RsourseChildAdapter extends BaseAdapter {
    private Context                           mContext;
    private List<SelectResourseBean.ListBean> mResourseChildList;
    private int                               mItemHeight;

    public RsourseChildAdapter(Context context, List<SelectResourseBean.ListBean> arrayList, int itemHeight) {
        mContext = context;
        mResourseChildList = arrayList;
        mItemHeight = itemHeight;
    }

    @Override
    public int getCount() {
        return mResourseChildList == null ? 0 : mResourseChildList.size();
    }

    @Override
    public Object getItem(int position) {
        return mResourseChildList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final SelectResourseBean.ListBean listBean = mResourseChildList.get(position);

        if (convertView == null) {

            convertView = parent.inflate(mContext, R.layout.resourse_child_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivGood = (ImageView) convertView.findViewById(R.id.iv_good);
            viewHolder.ivGoodBg = (ImageView) convertView.findViewById(R.id.iv_good_bg);
            viewHolder.ivSelect = (ImageView) convertView.findViewById(R.id.iv_selecet);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tvDetail = (TextView) convertView.findViewById(R.id.tv_details);
            convertView.setTag(viewHolder);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            convertView.setLayoutParams(params);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (listBean.isSelected()) {
            viewHolder.ivSelect.setVisibility(View.VISIBLE);
            viewHolder.ivGoodBg.setVisibility(View.VISIBLE);
        } else {
            viewHolder.ivSelect.setVisibility(View.INVISIBLE);
            viewHolder.ivGoodBg.setVisibility(View.INVISIBLE);
        }

        BaseApplication.getApplicationInstance().displayImg(listBean.getRelatedUrl(), viewHolder.ivGood);
        viewHolder.ivGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.showLog(listBean.toString());
                if (!listBean.isSelected()) {
                    if (!ContractCommon.getInstance().getSelectToCanSeePublishList().contains(listBean)) {
                        listBean.setSelected(true);
                        ContractCommon.getInstance().getSelectToCanSeePublishList().add(listBean);
                        EventBus.getDefault().post("SELECTNUMADD");
                    }
                } else {
                    Iterator it = ContractCommon.getInstance().getSelectToCanSeePublishList().iterator();
                    while (it.hasNext()) {
                        SelectResourseBean.ListBean next = (SelectResourseBean.ListBean) it.next();
                        if (next.getDefinedUkid() == listBean.getDefinedUkid()) {
                            it.remove();
                            listBean.setSelected(false);
                            EventBus.getDefault().post("SELECTNUMLESS");
                        }

                    }
                }
                notifyDataSetChanged();
            }
        });
        viewHolder.tvName.setText(listBean.getDefinedName());
        viewHolder.tvDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到详情页
                Bundle detailBundle = new Bundle();
                detailBundle.putString("itemType", mResourseChildList.get(position).getDefinedType());
                detailBundle.putString("type", "0");
                detailBundle.putLong("ukid", listBean.getDefinedUkid());
                ((BaseCardDeskActivity) mContext).pushFragment(
                        (Fragment) ARouter.getInstance().build("/ResourceCenter/InfomationResourcesFragment").with(detailBundle).navigation());
                LogUtils.showLog("详情" + listBean.getDefinedUkid() + "         " + position);
            }
        });

        return convertView;
    }


    static class ViewHolder {
        private ImageView ivGood;
        private ImageView ivGoodBg;
        private ImageView ivSelect;
        private TextView  tvName;
        private TextView  tvDetail;
    }
}
